<?php

namespace App\Services;

use App\Models\Article;
use App\Models\Recommend;
use Illuminate\Database\Eloquent\Model;

class ArticleService extends Model
{
    //
    private $_article;
    private $_systemService;
    private $_recommend;
    const   RECOMMEND_TYPE_SPECIALLY = 'SPECIALLY_RECOMMEND';
    const   RECOMMEND_TYPE_NORMAL = 'NORMAL_RECOMMEND';


    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        if (!$this->_article) {
            $this->_article = new Article();
        }
        if (!$this->_recommend) {
            $this->_recommend = new Recommend();
        }
        if (!$this->_systemService) {
            $this->_systemService = new SystemService();
        }
    }

    /**获取文章标题
     * @param $type
     * @return \Illuminate\Support\Collection
     */
    public function getArticleTitle($type)
    {
        $article_title_list = $this->_article->getArticleTitle($type);
        $param_list = $this->_systemService->getParams($this->_systemService::PARAMS_TYPE_SOURCE);
        foreach ($article_title_list as $k => &$v) {
            $v->source_name = $param_list->{$v->source};
        }
        return $article_title_list;
    }

    /**获取文章内容
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function getArticle($id)
    {
        $info = $this->_article->getArticle($id);
        if ($info) {
            $param_list = $this->_systemService->getParams($this->_systemService::PARAMS_TYPE_SOURCE);
            $info->source_name = $param_list->{$info->source};
        }
        return $info;

    }

    public function addReadNum($id)
    {
        return $this->_article->updateArticle($id);
    }

    /**点赞
     * @param $article_title_id
     * @return int
     */
    public function updateArticleLike($article_title_id)
    {
        $this->_article->updateArticleLike($article_title_id);
        $article = $this->getArticle($article_title_id);
        return $article->like;
    }

    /**获取推荐文章列表
     * @param $type
     * @return \Illuminate\Support\Collection
     */
    public function getArticleRecommend($type)
    {
        $list = $this->_recommend->getArticleRecommend($type);
        $param_list = $this->_systemService->getParams($this->_systemService::PARAMS_TYPE_SOURCE);
        foreach ($list as $k => &$v) {
            $v->source_name = $param_list->{$v->source};
        }
        return $list;
    }

    /**获取点击排行榜
     * @param $type
     * @return \Illuminate\Support\Collection
     */
    public function getArticleTitleTop($type)
    {
        $list = $this->_article->getArticleTitleTop($type);
        return $list;
    }
}
